package com.gl.spring.restTemplate;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created With IntelliJ IDEA
 * User: liuhj
 * Date: 14-7-11.
 */
public class RestClient {
    public static void main(String[] args) {
        RestClient c = new RestClient();
//        c.testJavaBean();//ok
//        c.testMap();//ok
//        c.testString();//ok
        c.testList();
    }

    /**
     * 传入的参数为字符串
     * 返回String、Object数据
     */
    public void testString() {
        RestTemplate restTemplate = new RestTemplate();
        String para = "201459";
        String s1 = restTemplate.postForObject("http://localhost:8080/search/testString?userId=" + para, null, String.class);
        System.out.println("testString s is :" + s1);
    }


    /**
     * 传入的参数为指定的javaBean
     * 返回String、Object数据
     */
    public void testJavaBean() {
        RestTemplate restTemplate = new RestTemplate();
//        MultiValueMap<String,String> dataMap = new LinkedMultiValueMap<String, String>();
        Student s = new Student();
        s.setName("jree");
        s.setAge(1);
        JSONObject jsonObject = JSONObject.fromObject(s);
        String s1 = restTemplate.postForObject("http://localhost:8080/search/testJavaBean", jsonObject, String.class);
        System.out.println("testJavaBean s is :" + s1);
    }

    /**
     * 传入的参数为指定的Map
     * 返回String、Object数据
     */
    public void testMap() {
        RestTemplate restTemplate = new RestTemplate();
//        MultiValueMap<String,String> dataMap = new LinkedMultiValueMap<String, String>();
        Map m = new HashMap();
        m.put("name", "myName1");
        m.put("address", "china1");
        Student s = new Student();
        s.setName("jree");
        s.setAge(1);
        List hobbies = new ArrayList();
        hobbies.add("bascktball");
        hobbies.add("football");
        s.setHobbies(hobbies);
        m.put("stu", s);
        JSONObject jsonObject = JSONObject.fromObject(m);
        System.out.print(jsonObject.toString());
        String result = restTemplate.postForObject("http://localhost:8080/search/testMap", jsonObject, String.class);
        System.out.println("testMap result is :" + result);
    }

    /**
     * 传入的参数为指定的List
     * 返回String、Object数据
     */
    public void testList() {
        RestTemplate restTemplate = new RestTemplate();
        List hobbies = new ArrayList();
        hobbies.add("bascktball");
        hobbies.add("football");
        JSONArray array = JSONArray.fromObject(hobbies);
        JSONObject object = array.toJSONObject(array);
        System.out.print(array.toString());
        String result = restTemplate.postForObject("http://localhost:8080/search/testList", object, String.class);
        System.out.println("testMap result is :" + result);
    }


}
